<script setup>
import { useRouter } from 'vue-router'
import { useStore } from '../store/store';

const store = useStore()

// 获取路由实例
const router = useRouter()
// 获取父组件传递的参数
const props = defineProps(['menuData', 'index'])

// console.log(props.menuData)

// 点击菜单
function handleClick(item, active){
  // console.log(item.meta.path)
  // 设置当前菜单选中状态
  item.meta.active = active
  // 添加菜单
  store.addMenu(item.meta)
  // 跳转路由
  router.push(item.meta.path)
  store.updateMenuActive(active)
}

</script>

<template>
  <template v-for="(item, index) in props.menuData">
    <el-menu-item 
      @click="handleClick(item, `${props.index}-${item.meta.id}`)"
      v-if="!item.children || item.children.length === 0" 
      :index="`${props.index}-${item.meta.id}`"
      :key="`${props.index}-${item.meta.id}`"
      >

      <el-icon size="20">
        <component :is="item.meta.icon"></component>
      </el-icon>
      <span>{{ item.meta.name }}</span>
    </el-menu-item>
    <el-sub-menu 
      v-else 
      :index="`${props.index}-${item.meta.id}`"
    >
      <template #title>
         <el-icon size="20">
            <component :is="item.meta.icon"></component>
          </el-icon>
        <span>{{ item.meta.name }}</span>
      </template>
      <treeMenu 
        :index="`${props.index}-${item.meta.id}`"
        :menu-data="item.children"
        />

    </el-sub-menu>

  </template>
</template>

<style scoped></style>
